home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / task1.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  7KB  |  222 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: task1.c,v 1.4 1997/07/09 13:26:14 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:     task1.c
  35.  *
  36.  * This program will seek out a "well known" service from a mailbox
  37.  * and then initiate communication with that service via the
  38.  * mailbox "message" specified communication context.
  39.  *
  40.  * This is designed to be used along with task0.c as they have
  41.  * coordinated mailbox message formats.
  42.  *
  43.  *    usage: task1 [service_name] <index> <flags>
  44.  *
  45.  *                PvmMboxDefault        d
  46.  *                PvmMboxFirstAvail    f
  47.  *                PvmMboxReadAndDelete    r
  48.  *
  49.  *  Note: A task started by task0.c is waiting for someone to
  50.  *  communicate via mailbox information.  If task1 communicates via
  51.  *    a PvmMboxReadAndDelete - the mailbox entry will be removed but
  52.  *    the waiting task will be left to wait indefinately since no
  53.  *    longer is the associated mailbox information available.
  54.  *    Additional code is required to clean-up waiting task too -
  55.  *    but that is not the intent of this code example...
  56.  *
  57.  *  files used:  task1.c taskf.c
  58.  */
  59.  
  60. #include <stdio.h>
  61. #ifndef WIN32
  62. #include <unistd.h>        /* for gethostname */
  63. #else
  64. #include "pvmwin.h"
  65. #endif
  66. #include "pvm3.h"
  67.  
  68. main( argc, argv )
  69. int argc;
  70. char *argv[];
  71. {
  72.     char *me = "task1";
  73.     char machine[25];        /* local machine that task1 is running on */
  74.     int mytid;                /* tid of task1 */
  75.     int my_context;            /* context of task1 - itself */
  76.     int i;
  77.     int index = 0;            /* index of mailbox message */
  78.  
  79.     char *service_name;        /* name of the service seeking */
  80.  
  81.     int their_context;        /* context of the service's owner */
  82.                             /*   - comm on this context */
  83.     int msg_buf;            /* buffer to store mailbox's message */
  84.     int their_tid;            /* tid "offering" the desired service */
  85.     char server[25];        /* name of machine "offering" the service */
  86.  
  87.     char msg_txt[100];        /* message to send to service */
  88.  
  89.     char flagc[10];            /* hold characters for flags */
  90.     int flags;                /* PvmMbox flags set */
  91.     char *flagstring;        /* pointer to string of flag settings */
  92.  
  93.     /* display_incomming_parameters( me, argc, argv ); */
  94.  
  95.     /*
  96.      *  validate input parameters
  97.      */
  98.     if ( argc == 4 ) {
  99.         /* we got what we wanted -- all three */
  100.         strcpy( flagc, argv[3] );        /* copy flags to local */
  101.         index = atoi( argv[2] );        /* get the index value */
  102.     }
  103.     else {
  104.         if ( argc == 2 ) {
  105.             /* assume index and flags left off */
  106.             /*   - so force index = 0, flags = PvmMboxDefault */
  107.             strcpy( flagc, "d" );
  108.             index = 0;
  109.         }
  110.         else{
  111.             /* unknown input sequence - complain and exit */
  112.             printf( "\n\nusage: task1 [service_name] <index> <flags>\n" );
  113.             printf( "\tPvmMboxDefault\t\td\n" );
  114.             printf( "\tPvmMboxFirstAvail\tf\n" );
  115.             printf( "\tPvmMboxReadAndDelete\tr\n\n" );
  116.             exit( -1 );
  117.         }
  118.     }
  119.  
  120.     /* printf( "%s: sending <%s> to set_flags.\n", me, flagc ); */
  121.     if ( ( flags = set_flags( &flagc, &flagstring ) ) < 0 ) {
  122.         exit( -1 );    /* something failed - abort! */
  123.     }
  124.  
  125.     service_name = argv[1];
  126.     printf( "%s: Looking for service name <%s>, index %d ",
  127.             me, service_name, index );
  128.     printf( "- my flags are <%d> : <%s>.\n", flags, flagstring );
  129.  
  130.     if ( ( mytid = pvm_mytid() ) == PvmSysErr ) {
  131.         printf( "\nPVM not up!\n" );
  132.         exit( -1 );
  133.     }
  134.  
  135.     gethostname( machine, 25 );
  136.  
  137.     printf( "%s: t%x on machine <%s> with context %d.\n",
  138.             me, mytid, machine, pvm_getcontext() );
  139.  
  140.     /*
  141.      *  look for the user specified service name
  142.      */
  143.     msg_buf = pvm_recvinfo( service_name, index, flags );
  144.     if ( msg_buf >= 0 ) {
  145.         pvm_setrbuf( msg_buf );
  146.         pvm_upkint( &their_tid, 1, 1 );
  147.         pvm_upkint( &their_context, 1, 1 );
  148.         pvm_upkstr( server );
  149.  
  150.         /*
  151.          *  Note that this displays the "index" requested which may
  152.          *  not be the actual one of that returned if PvmMboxFirstAvail
  153.          *  flag is set...
  154.          */
  155.         printf( "%s: service name <%s> and index %d with flags <%s> ",
  156.                 me, service_name, index, flagstring );
  157.         printf( "tid: %x and with context: %d\n",
  158.                 their_tid, their_context );
  159.     }
  160.     else {
  161.         switch ( msg_buf )
  162.         {
  163.             case PvmNotFound:
  164.                 printf( "\n%s: no service running\n", me );
  165.                 break;
  166.             case PvmBadParam:
  167.                 printf( "\n%s: Invalid argument to pvm_recvinfo().\n",
  168.                         me );
  169.                 break;
  170.             case PvmNoSuchBuf:
  171.                 printf( "\n%s: Message buffer id does not exist.\n",
  172.                         me );
  173.                 break;
  174.             case PvmDenied:
  175.                 printf(
  176.                     "\n%s: Key locked by another task, can't delete.\n",
  177.                         me );
  178.                 break;
  179.             default:
  180.                 lpvmerr( "task1.c: error", msg_buf );
  181.                 break;
  182.         } /* end_switch */
  183.         exit( -1 );
  184.     }
  185.  
  186.     /*
  187.      *  send message to the service with their context set
  188.      */
  189.  
  190.     /* activate the server's context */
  191.     my_context = pvm_setcontext( their_context );
  192.  
  193.     printf( "%s: t%x on machine <%s> with context %d.\n",
  194.             me, mytid, machine, pvm_getcontext() );
  195.  
  196.     sprintf( msg_txt, "\t%s t%x on machine <%s> with context %d.",
  197.             me, mytid, machine, pvm_getcontext() );
  198.     pvm_initsend( PvmDataDefault );
  199.     pvm_pkstr( msg_txt );
  200.     pvm_send( their_tid, 1 );
  201.  
  202.     /*
  203.      *  receive message from service
  204.      */
  205.     msg_buf = pvm_recv( their_tid, -1 );
  206.     pvm_bufinfo( msg_buf, (int *) 0, (int *) 0, &their_tid );
  207.     pvm_upkstr( msg_txt );
  208.     printf( "%s: t%x  from task t%x %s\n",
  209.             me, mytid, their_tid, msg_txt );
  210.  
  211.     /*
  212.      *  reset back to my original context
  213.      */
  214.     pvm_setcontext( my_context );
  215.     printf( "%s: t%x on machine <%s> with context %d.\n",
  216.             me, mytid, machine, pvm_getcontext() );
  217.  
  218.     pvm_exit();
  219.     exit( 0 );
  220. }
  221.  
  222.